clear
use ${data}preference_data.dta
drop if outside_cm==1 | outside_sch==1


merge m:1 teacher_id schoolcert_id using ${data}Simulation/teacher_uhats.dta,  nogen assert(3)
merge m:1 teacher_id schoolcert_id using ${data}Simulation/school_uhats.dta,  nogen assert(3)

* Limit to teacher's first interview day
keep if intDay==firstDay // restrict to teacher's first interview day



* Expand so one row per position
gen row=_n

expand NumberPositions
sort schoolcert_id row
by schoolcert_id row: gen pos = _n
drop row
egen schoolpos_id = group(schoolcert_id pos)
*bys schoolpos_id: egen schoolpos_seq=seq()
	
	
* Groups for outside option draws
egen group = group(majority_black cert_group)
bys group: egen group_seq=seq()
bys group intDay: egen groupDay_seq=seq()


keep teacher_id school_id schoolcert_id schoolpos_id NumberPositions pos intDay round  uhat_teacher* uhat_sch* _outside_option_bs*  group group_seq groupDay_seq

save ${data}Simulation/sim_data.dta, replace
	


* Generate Utility Draws for Simulations 
forv bs=0/$bootstraps {
forv d=1/3 {
	
	clear
	use ${data}Simulation/sim_data.dta  if intDay==`d' 
	
	* Draw teacher  utilities
	gen epsilon_t =  -ln(-ln(runiform())) 
	gen u_t_oo_low = uhat_teacher_oo_low`bs' +epsilon_t 
	gen u_t_oo_high = uhat_teacher_oo_high`bs' +epsilon_t
	gen u_t_oo_mid = uhat_teacher_oo_mid`bs' +epsilon_t
	drop epsilon_t
	
	gen epsilon_t =  -ln(-ln(runiform()))
	gen outside_t = epsilon_t
	drop epsilon_t
		
		
	gsort  teacher_id -u_t_oo_low
	by teacher_id: gen Rank_CM_true_oo_low = _n 
		
	by teacher_id: egen median = median(u_t_oo_low)
	gen above = u_t_oo_low>=median
	gen random = runiform()
	gsort teacher_id -above random
	by teacher_id: gen Rank_CM_tier_oo_low = _n
	drop above random median
		
		
	gsort  teacher_id -u_t_oo_high
	by teacher_id: gen Rank_CM_true_oo_high = _n 
		
	by teacher_id: egen median = median(u_t_oo_high)
	gen above = u_t_oo_high>=median
	gen random = runiform()
	gsort teacher_id -above random
	by teacher_id: gen Rank_CM_tier_oo_high = _n
	drop above random median		
		
		
	gsort  teacher_id -u_t_oo_mid
	by teacher_id: gen Rank_CM_true_oo_mid = _n 
		
	by teacher_id: egen median = median(u_t_oo_mid)
	gen above = u_t_oo_mid>=median
	gen random = runiform()
	gsort teacher_id -above random
	by teacher_id: gen Rank_CM_tier_oo_mid = _n
	drop above random median
		
	
	* Draw school utilities
	gen epsilon =  -ln(-ln(runiform())) 
	gen u_sch=uhat_sch`bs'+epsilon 
	drop epsilon
	
		
	* Draw 10 school outside options	
	forv o=1/10 {

		gen _epsilon = -ln(-ln(runiform()))  if groupDay_seq==1
		* Note only 1 draw per group 
		bys group intDay: egen sch_epsilon =  max(_epsilon) 
		gen sch_outside_option`o' = _outside_option_bs`bs'+sch_epsilon 
		drop sch_epsilon _epsilon 
		
		

	
	}	
	drop _outside_option_bs*	uhat_teacher_oo_* uhat_sch*
	
	reshape long sch_outside_option, i(teacher_id schoolpos_id) j(oo)
	
	* School ranks, only for acceptable teachers
	gsort oo schoolpos_id -u_sch
	by oo schoolpos_id: gen Rank_School = _n if u_sch > sch_outside_option & u_sch<. 
	
	save ${data}Simulation/sim_data_d`d'_bs`bs'.dta, replace
 }
 }
 
 
 
 exit
